package com.excel.mq;


import com.corpgovernment.common.mq.enums.MessageBizTypeEnum;
import com.corpgovernment.common.mq.producer.CommonMQProducer;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.excel.mq.Bo.EmployEventBo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ExcelMQProducer {

    @Autowired
    private CommonMQProducer commonMQProducer;

    public boolean sendEmployeeBasic(EmployEventBo employEventBo) {
        try {
            Message message = new Message();
            message.setTopic(MessageBizTypeEnum.ORG_EXCEL.getTopic());
            message.setBody(JsonUtils.toJsonString(employEventBo).getBytes());
            message.setTags(employEventBo.getTags());
            SendResult result = commonMQProducer.send(message);
            if (result != null && result.getSendStatus() == SendStatus.SEND_OK) {
                log.info("员工主体消息发送成功", JsonUtils.toJsonString(result));
                return true;
            }
        } catch (Exception e) {
            log.error("员工主体消息发送失败:{}", e.getMessage(), e);
        }
        return false;
    }

    public boolean sendEmployeeCheckDelay(EmployEventBo employEventBo,Integer delayLevel) {
        try {
            Message message = new Message();
            message.setTopic(MessageBizTypeEnum.ORG_EXCEL_DELAY_ACK.getTopic());
            message.setBody(JsonUtils.toJsonString(employEventBo).getBytes());
            //设置消费时间RocketMq所支持的定时时间是有限的默认Broker服务器端有18个定时级别，每一个级别分别对应不同的延迟时间
            /*1-1s|2-5s|3-10s|4-30s|5-1m|6-2m|7-3m|8-4m|9-5m|10-6m|
            11-7m|12-8m|13-9m|14-10m|15-20m|16-30m|17-1h|18-2h*/
            message.setDelayTimeLevel(delayLevel);
            message.setTags(MessageBizTypeEnum.ORG_EXCEL_DELAY_ACK.getTags());
            SendResult result = commonMQProducer.send(message);
            if (result != null && result.getSendStatus() == SendStatus.SEND_OK) {
                log.info("员工主体消息发送成功", JsonUtils.toJsonString(result));
                return true;
            }
        } catch (Exception e) {
            log.error("员工主体消息发送失败:{}", e.getMessage(), e);
        }
        return false;
    }
}
